using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpaceShooter.Common;

namespace SpaceShooter.GameObjects
{
    public class Player : GameObject
    {
        private MissileLauncher launcher;
        private int score;
        private int ammunition;
        private KeyStrokeTable keyStrokeTable;
        private ControlState lastControlState;

        public Player(IGameScreen gameScreen)
            : base(gameScreen, @"Models\p1_wedge")
        {
            this.launcher = new MissileLauncher(this.GameScreen, this, () => MissileLaunchBehavior());
            this.launcher.LaunchOffset = new Vector3(0f, -100f, 1000f);
            this.launcher.TimeBetweenShots = TimeSpan.FromMilliseconds(500);
            this.launcher.MinTimeBetweenShots = TimeSpan.FromMilliseconds(250);

            this.score = 0;
            this.ammunition = 100;
        }

        public int Ammunition
        {
            get { return this.ammunition; }
        }

        public int Score
        {
            get { return this.score; }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            this.lastControlState = GameControlHelper.GetKeyPressedState(this.lastControlState, this.keyStrokeTable);

            if (this.keyStrokeTable.FlyLeft.IsActive) this.AddPosition(new Vector3(-3000.0f * (float)gameTime.ElapsedGameTime.TotalSeconds, 0, 0));
            if (this.keyStrokeTable.FlyRight.IsActive) this.AddPosition(new Vector3(+3000.0f * (float)gameTime.ElapsedGameTime.TotalSeconds, 0, 0));
            if (this.keyStrokeTable.FlyForward.IsActive) this.AddPosition(new Vector3(0, 0, -3000.0f * (float)gameTime.ElapsedGameTime.TotalSeconds));
            if (this.keyStrokeTable.FlyBack.IsActive) this.AddPosition(new Vector3(0, 0, +3000.0f * (float)gameTime.ElapsedGameTime.TotalSeconds));
            if (this.keyStrokeTable.ShootWeapon.IsActive) this.launcher.Launch();

            this.launcher.Update(gameTime);
        }


        public bool MissileLaunchBehavior()
        {
            if (this.ammunition < 10)
            {
                // not enough ammunitition
                return false;
            }

            this.ammunition -= 10;

            return true;
        }

        public void AddAmmunitionHitBonus()
        {
            this.ammunition += 20;
        }

        public void AddScore(int bonus)
        {
            if (bonus < 0)
            {
                throw new ArgumentException("Bonus must 0 or greater!");
            }

            this.score += bonus;
        }

        public void SetKeystrokeTable(KeyStrokeTable table)
        {
            this.keyStrokeTable = table;
        }
    }
}
